#! /usr/bin/env python
"""
    Adelide - A virtual media bookshelf.
    Copyright (C) Niklas Johansson - jonikl@ituniv.se, Anne-Katrin Krolovitsch - annekatr@ituniv.se

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
"""
import unittest

import sys
sys.path.append('../db')
sys.path.append('../adelib')


from lib import coverage
from lib import cProfile
from lib import pstats
import datetime
import databaseHandler
from adelib import media

class testAdd(unittest.TestCase):
    def setUp(self):
        coverage.start()
        self.db = databaseHandler.databaseHandler()

    
    def testAddMedia(self):
        new_media = media.Media()
        new_media.title = "Hackers"
        new_media.year = 1998
        new_media.cover = "none"
        new_media.ean = "123344322"
        new_media.rating = 1
        new_media.favorite = 1
        new_media.format_id = 2
        new_media.type_id = 1
        new_media.studio_id = 3
        new_media.publisher_id = 2
        
       # self, media_id, title, year, cover, ean, rating, favorite, format_id, type_id, studio_id, publisher_id, tags, humans
        self.db.add_to_db(new_media)
        cursor = self.db.execute_query('SELECT * FROM media WHERE title = "Hackers"')
        for result_med in cursor:
            assert result_med.title == "Hackers"
    
    def tearDown(self):
        coverage.stop()

class testDelete(unittest.TestCase):
    def setUp(self):
        coverage.start()
        self.db = databaseHandler.databaseHandler()

    def testDeleteMedia(self):
        self.db.delete_media(1)
        cursor = self.db.execute_query('SELECT * FROM media WHERE id = 1')
        
        assert len(cursor) == 0
        
    def tearDown(self):
        coverage.stop()
        
class testSearch(unittest.TestCase):
    def setUp(self):
        coverage.start()
        self.db = databaseHandler.databaseHandler()
        
        new_media = media.Media()
        new_media.title = "Track"
        new_media.year = 1998
        new_media.cover = "none"
        new_media.ean = "123344322"
        new_media.rating = 1
        new_media.favorite = 1
        new_media.format_id = 2
        new_media.type_id = 1
        new_media.studio_id = 3
        new_media.publisher_id = 2
        self.db.add_to_db(new_media)
        
        #negative testing
    def testNegSearchMedia(self):
        cursor = self.db.do_search("Some random film") 
        assert cursor == []
        
    def testPosSearchMedia(self):
        cursor = self.db.do_search("Track")
        for res_media in cursor:
            assert res_media.title == "Track"
        
    def tearDown(self):
        self.db.delete_media(1)
        coverage.stop()
        
if __name__ == "__main__":
    coverage.start()
    runner = unittest.TextTestRunner()
    loader = unittest.TestLoader()
    runner.run(loader.loadTestsFromTestCase(testAdd))
    runner.run(loader.loadTestsFromTestCase(testDelete))
    runner.run(loader.loadTestsFromTestCase(testSearch))
    filename = str(datetime.date.today())
    coverage.report(["../db/databaseHandler.py", "function_testing.py"])
    cProfile.run('runner.run(loader.loadTestsFromTestCase(testAdd))', 'addprof')
    add_stats = pstats.Stats('addprof')
    cProfile.run('runner.run(loader.loadTestsFromTestCase(testDelete))', 'deleteprof')
    delete_stats = pstats.Stats('deleteprof')
    cProfile.run('runner.run(loader.loadTestsFromTestCase(testSearch))', 'searchprof')
    search_stats = pstats.Stats('searchprof')
    add_stats.print_stats()
    delete_stats.print_stats()
    search_stats.print_stats()
    coverage.stop()
